package com.aitaoxue.palx.adapter;

import java.util.HashMap;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.base.ListBaseAdapter;
import com.aitaoxue.palx.bean.SearchParent;

public class PullOrderAdapter extends ListBaseAdapter {
	public static final String TAG = PullOrderAdapter.class.getSimpleName();

	private HashMap<Integer, Boolean> map;

	public PullOrderAdapter() {
		map = new HashMap<Integer, Boolean>();
	}

	@Override
	protected View getRealView(final int position, View convertView,
			ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null || convertView.getTag() == null) {
			convertView = LayoutInflater.from(parent.getContext()).inflate(
					R.layout.list_cell_pull_order, null);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (!map.containsKey(position)) {
			map.put(position, false);
		}

		SearchParent item = (SearchParent) _data.get(position);

		Log.d(TAG, position + "");

		viewHolder.city.setText(item.getCity_name());
		viewHolder.course.setText(item.getGood_kind_name());
		viewHolder.name.setText("家长姓名: " + item.getName());
		viewHolder.price.setText("价格:" + item.getPrice());
		viewHolder.timeView.setText(item.getPost_time() + "发布    " + "浏览"
				+ item.getRequirement_visitor() + "次");

		switch (item.getHasConnect()) {
		case "1":
			viewHolder.status.setText("已联系");
			//viewHolder.checkBox.setVisibility(View.GONE);
			break;
		case "0":
			viewHolder.status.setText("未联系");
			break;
		case "2":
			viewHolder.status.setText("已经找到家教");
			break;
		default:
			break;
		}

		viewHolder.checkBox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						Log.d(TAG, "changed:" + position);
						if (isChecked) {
							map.put(position, true);
						} else {
							map.put(position, false);
						}
					}
				});

		viewHolder.checkBox.setChecked(map.get(position));

		return convertView;
	}

	class ViewHolder {
		@InjectView(R.id.checkbox)
		CheckBox checkBox;
		@InjectView(R.id.course)
		TextView course;
		@InjectView(R.id.city)
		TextView city;
		@InjectView(R.id.price)
		TextView price;
		@InjectView(R.id.time_view)
		TextView timeView;
		@InjectView(R.id.name)
		TextView name;
		@InjectView(R.id.status)
		TextView status;

		public ViewHolder(View view) {
			ButterKnife.inject(this, view);
		}
	}

	public HashMap<Integer, Boolean> getMap() {
		return map;
	}

	public void clear() {
		map.clear();
	}

}
